package zim;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Dataset {

	private List<List<Integer>> data = new ArrayList<>();
	/**
	 * liczba cech
	 */
	private int charCount;

	public void loadData(File file, int charCount) throws FileNotFoundException {
		this.charCount = charCount;
		Scanner scanner = new Scanner(file);
		scanner.useDelimiter("\n");
		List<Integer> lineData;
		while (scanner.hasNext()) {
			lineData = new ArrayList<>();
			Scanner lineScanner = new Scanner(scanner.next());
			lineScanner.useDelimiter(",");

			while (lineScanner.hasNextInt()) {
				lineData.add(lineScanner.nextInt());
			}
			data.add(lineData);

		}
	}

	public int getCount() {
		return data.size();
	}

	public List<List<Integer>> getData() {
		return data;
	}

	public int getCharCount() {
		return charCount;
	}
	
	public void setCharCount(int charCount) {
		this.charCount = charCount;
	}
	
	public Dataset divide() {
		Dataset newDataset = new Dataset();
		Random randomGenerator = new Random();
		int n = getCount()/2;
		for (int i = 0; i < n; i++) {
			int random = randomGenerator.nextInt(getCount());
			newDataset.getData().add(data.remove(random));
		}
		newDataset.setCharCount(charCount);
		return newDataset;
	}
	
	public HashMap<Integer, Integer> generateClasses() {
		HashMap<Integer, Integer> classes = new HashMap<>();
		for (int i = 0; i < data.size(); i++) {
			int temp = data.get(i).get(data.get(i).size()-1);
			int value = 0;
			if (classes.containsKey(temp))
				value = classes.get(temp);
			classes.put(temp, ++value);
		}
		return classes;
	}
	

}
